package utils;
import java.io.UnsupportedEncodingException;
/*
 * 异或加解密算法
 */
import java.nio.charset.Charset;
import java.util.HashMap;
import java.util.Map;  

public class DeEnCode {  
    private static final String key0 = "SHAXIAOSENG*20160118-12:00#luoke$";  
    private static final Charset charset = Charset.forName("UTF-8");  
    private static byte[] keyBytes = key0.getBytes(charset); 
    
	/**
	 * 加密过程      
	 * @param enc 数据
	 * @return
	 */
    public static String encode(String enc){  
        byte[] b = enc.getBytes(charset);  
        for(int i=0,size=b.length;i<size;i++){  
            for(byte keyBytes0:keyBytes){  
                b[i] = (byte) (b[i]^keyBytes0);  
            }  
        }  
        return new String(b);  
    }
    
    /**
     * 解密过程      
     * @param dec 数据
     * @return
     */      
    public static String decode(String dec){  
        byte[] e = dec.getBytes(charset);  
        byte[] dee = e;  
        for(int i=0,size=e.length;i<size;i++){  
            for(byte keyBytes0:keyBytes){  
                e[i] = (byte) (dee[i]^keyBytes0);  
            }  
        }  
        return new String(e);  
    }

    /**
     * 二次加密过程      
     * @param data 数据
     * @return
     */  
    public static String quadraticENC(String data){
		data = DeEnCode.encode(data);
		data = Base64.encode(data.getBytes());
		return data;
    }
    
    /**
     * 二次解密过程      
     * @param data 数据
     * @return
     */  
    public static String quadraticDEC(String data){
    	try {
			byte[] indata = Base64.decode(data);
			String srt = new String(indata,"UTF-8");
			data = DeEnCode.decode(srt);
		} catch (UnsupportedEncodingException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			data = "";
		}
		return data;
    }
    
    /**
     * 拆解URL形加密参数过程      
     * @param data 数据
     * @return
     */  
    public static Map<String, String> DismantURL(String data){
    	Map<String, String>	map = new HashMap<String, String>();
    	if(data != null && !"".equals(data)){
			String[] strA = data.split("&");
			for(int i=0; i< strA.length; i++){
				String[] strB = strA[i].split("=");
				map.put(strB[0], strB[1]);
			}
    	}
		return map;
    }     
}  